package com.stars.questions.deptQuestion.init.service.impl;

import com.stars.questions.deptQuestion.config.entity.Question;
import com.stars.questions.deptQuestion.config.entity.QuestionSub;
import com.stars.questions.deptQuestion.config.service.QuestionConfigService;
import com.stars.questions.deptQuestion.init.service.InitService;
import com.stars.questions.deptQuestion.publish.entity.Reply;
import com.stars.questions.deptQuestion.publish.entity.ReplySub;
import com.stars.questions.deptQuestion.publish.mapper.PublishMapper;
import com.stars.questions.deptQuestion.publish.mapper.ReplySubMapper;
import com.stars.util.common.DevelopKit;
import org.springframework.data.annotation.Transient;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author : Rocks
 * @version : 1.0
 * @date : 2022/12/3 23:16
 * @description : 初始化数据服务实现类
 */
@Service
public class InitServiceImpl implements InitService {

    private final QuestionConfigService configService;
    private final PublishMapper publishMapper;
    private final ReplySubMapper replySubMapper;

    public InitServiceImpl(QuestionConfigService configService, PublishMapper publishMapper, ReplySubMapper replySubMapper) {
        this.configService = configService;
        this.publishMapper = publishMapper;
        this.replySubMapper = replySubMapper;
    }

    @Override
    public boolean initReply(int count) {

        Question question = configService.loadQuestion("d1c99d445df248d69cb26bacde08ad60");
        List<QuestionSub> questionSubs = configService.loadQuestionSub(question.getId(), question.getMark());

        for(int i = 0;i<count;i++){
            if(!this.saveReply(questionSubs)){
                return false;
            }
        }

        return true;
    }

    @Transient
    private boolean saveReply(List<QuestionSub> questionSubs){
        try{
            Reply reply = new Reply();
            String replyId = DevelopKit.generateUUID();

            reply.setId(replyId);
            reply.setConfigId("d1c99d445df248d69cb26bacde08ad60");
            reply.setYear("2022");
            reply.setUserName(this.initName());
            reply.setAge(Integer.toString((int) (40 * Math.random())));
            reply.setMobile(Integer.toString((int) (100000000 * Math.random())));
            DevelopKit.setBaseInfo(reply);

            publishMapper.insert(reply);

            int j = 1;
            for(QuestionSub sub : questionSubs){
                ReplySub replysub = new ReplySub();

                replysub.setId(DevelopKit.generateUUID());
                replysub.setFid(replyId);
                replysub.setQuestionId(sub.getId());

                String[] answers = this.initAnswer(sub.getId());

                replysub.setAnswer(answers[0]);
                replysub.setAnswerText(answers[1]);
                replysub.setConfigId("d1c99d445df248d69cb26bacde08ad60");
                replysub.setQuestion(sub.getQuestion());
                replysub.setShowOrder(j++);
                replysub.setYear("2022");
                DevelopKit.setBaseInfo(replysub);

                replySubMapper.insert(replysub);
            }

        }catch (Exception e){
            return false;
        }
        return true;
    }




    private String initName(){

        String[] firstNames = {"汪","吴","彭","鲁","任","罗","何","黄","段","缪","柳","秦","蒋","申","叶","王","考卡尔汗·","郭","张","辜","雷","米然·","曹","刘","靳","徐","李","孙","陈","夏","马","郝","殷","明","唐","林","吉","苏","施","武","常","米","阮","翟"};
        String[] secondNames = {"兵","心迪","居买","红梅","丹","莹","晓玲","令忠","杨","浩","柯","力坤","芹","晶晶","彦军","建强","晶","建冮","佳维","强","鸿","亚琼"
                ,"震强","玉良","海玉","江涛","连江","锋","俊韬","斌","倩","帅","博文","永兵","文新","丽","艳","小萍","月","凯雯","凤霞","建峰","璇","文峰","瑞","俊","玉兵"
                ,"延平","勇亮","霞","萍萍","德强","燕超","庆","明博","灵灵","世涛","一菲","嘉钰","印亭","燕","清","永辉","娟娟","德磊","曼斐","建胜","龙","玉霞","疆川","志刚"
                ,"学海","玉卿","文杰","荣乐","成玉","代强","训昭","勇","晖","进军","楠楠","用志","利","莹梅","龙刚","传立","艳美","涛","倩玉","冬玲","震洪"};
        String firstName = firstNames[(int) (firstNames.length*Math.random())];
        String secondName = secondNames[(int)(secondNames.length*Math.random())];

        return firstName+secondName;
    }

    private String[] initAnswer(String questionId){

        String[] result = new String[2];
        int[] scores = {6,8,8,8,8,10};
        int score;
        String scoreText;
        if(questionId.equals("aebc5e63f8aa4889949fe70732983852")
                ||questionId.equals("9d90281cc73e4a1a944dfc6ed2b3c219")
                ||questionId.equals("42abf7aaf31b4852990d6f1fdb31142e")){
            scores = new int[]{4, 6 ,8, 8, 8};
        }
        if(questionId.equals("a922a9c8852746969729347d0c4b81a0")
                ||questionId.equals("2075c59ea7a94774852287a9b0055428")){
            scores = new int[]{8, 8, 10 ,10 ,10 };
        }

        score = scores[(int)(scores.length*Math.random())];

        if(score==0){
            scoreText = "较差";
        }else if(score==4){
            scoreText = "一般";
        }else if(score==6){
            scoreText = "较好";
        }else if(score==8){
            scoreText = "良好";
        }else{
            scoreText = "优秀";
        }

        result[0] = score+"";
        result[1] = scoreText;
        return result;
    }
}
